#include "MonitoringDeviceSqLite.h"
#define SQNAME "userMs.db"
MonitoringDeviceSqLite::MonitoringDeviceSqLite(QObject *parent) : QObject(parent)
{
    db = QSqlDatabase::addDatabase("QSQLITE", "myConnection");
    db.setDatabaseName(SQNAME);
    query = QSqlQuery(db);
}
MonitoringDeviceSqLite& MonitoringDeviceSqLite::getInstance() {
    static MonitoringDeviceSqLite instance;
    return instance;
}
MonitoringDeviceSqLite::~MonitoringDeviceSqLite()
{
    if (db.isOpen()) {
        db.close();
    }
}
bool MonitoringDeviceSqLite::openSqLite(const QString &dbPath)
{
    // 加载 SQLite 数据库驱动
    //    db= QSqlDatabase::addDatabase("QSQLITE");
    //    // 设置数据库文件名，如果文件不存在会自动创建
    //    db.setDatabaseName(dbPath);

    // 打开数据库连接
    if (!db.open()) {
        qDebug() << "无法打开数据库:" << db.lastError().text();
        return false;
    }
    qDebug() << "数据库已成功打开";
    query=QSqlQuery(db);
    return  true;
}
bool MonitoringDeviceSqLite::createSqLite(const QString &sql)
{

    // 创建一个名为 "users" 的表
    if (!query.exec(sql)) {
        qDebug() << "创建表失败:" << query.lastError().text();
        return false;
    }
    return true;

}

QueryResult MonitoringDeviceSqLite::insertData(const QString&user_id,const QString &name, const QString &type, const QString &location, const QString &manufacturer, const QString &model, const QString &status)
{
    if(!optionsSqLite()){
        return QUERY_FAILED;
    }
        const QString insertSql = "INSERT INTO devicemanager (user_id,name, type, location, manufacturer,model,status,installation_date) VALUES (:user_id,:name, :type, :location, :manufacturer, :model ,:status,:installation_date)";
        query.prepare(insertSql);
        query.bindValue(":user_id", user_id);
        query.bindValue(":name", name);
        query.bindValue(":type", type);
        query.bindValue(":location", location);
        query.bindValue(":manufacturer", manufacturer);
        query.bindValue(":status", status);
        query.bindValue(":model", model);
        query.bindValue(":installation_date",  MonitoringTool::getNowTimes());
        db.transaction();
        if (query.exec()) {
            qDebug() << "数据插入成功";
            db.commit();
            // db.close();
            return QUERY_SUCCESS;
        } else {
            qDebug() << "数据插入失败:" << query.lastError().text();
            db.rollback();
            // db.close();
            return QUERY_FAILED;
        }
    }


bool MonitoringDeviceSqLite::optionsSqLite()
{
    if(!openSqLite(SQNAME)){
        // db.close();
        return false;
    }
    if(!createSqLite("CREATE TABLE IF NOT EXISTS devicemanager ("
                     "id INTEGER PRIMARY KEY AUTOINCREMENT, "
                     "user_id TEXT NOT NULL, "
                     "name TEXT NOT NULL, "
                     "type TEXT NOT NULL, "
                     "location TEXT, "
                     "manufacturer TEXT NOT NULL,"
                     "model TEXT, "
                     "status TEXT, "
                     "installation_date TEXT"
                     ")")){
        // db.close();
        return false;
    }
    return true;
}
